/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ClientRMI;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import java.awt.Point;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
//import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
import shared.*;


public class MyGraphicsView extends JPanel implements MouseListener, KeyListener
{

    CopyOnWriteArrayList<Bullet> bulletsList = new CopyOnWriteArrayList<Bullet>();
    CopyOnWriteArrayList<Player> playersList = new CopyOnWriteArrayList<Player>();
    Player player = new Player(new Point.Double(100, 100), 0);
    Map map = new Map();
    int mouseX = 0;
    int mouseY = 0;


    public MyGraphicsView()
    {
        new RepeatingReleasedEventsFixer().install();
        
        setPreferredSize(new Dimension(600, 600));
        this.setFocusable(true);
        addMouseListener(this);
        addKeyListener(this);

    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        map.draw(g2d);
        
        for (Player play : playersList)
            play.draw(g2d);
   
        for (Bullet bull : bulletsList)
            bull.draw(g2d);

    }

    public void mouseClicked(MouseEvent e)
    {

    }

    public void mousePressed(MouseEvent e)
    {
        player.shoot = true;
        mouseX = e.getX();
        mouseY = e.getY();
    }

    public void mouseReleased(MouseEvent e)
    {
        player.shoot = false;
        mouseX = 0;
        mouseY = 0;
    }

    public void mouseEntered(MouseEvent e)
    {

    }

    public void mouseExited(MouseEvent e)
    {

    }

    public void keyTyped(KeyEvent e)
    {

    }

    public void keyPressed(KeyEvent e)
    {

        switch (e.getKeyCode())
        {
            case KeyEvent.VK_A:
                player.moveLeft = true;
                break;

            case KeyEvent.VK_D:
                player.moveRight = true;
                break;

            case KeyEvent.VK_W:
                player.moveUp = true;
                break;

            case KeyEvent.VK_S:
                player.moveDown = true;
                break;
        }
    }

    public void keyReleased(KeyEvent e)
    {
        switch (e.getKeyCode())
        {
            case KeyEvent.VK_A:
                player.moveLeft = false;
                break;

            case KeyEvent.VK_D:
                player.moveRight = false;
                break;

            case KeyEvent.VK_W:
                player.moveUp = false;
                break;

            case KeyEvent.VK_S:
                player.moveDown = false;
                break;
        }

    }

}
